package top.maochang.hou;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class HelloWorld {
    /**
     * IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
     * SecurityManager securityManager = factory.getInstance();
     * ————————————————
     * DefaultSecurityManager securityManager = new DefaultSecurityManager();
     * IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
     * securityManager.setRealm(iniRealm);
     */
    @Test
    public  void   shiroLogin(){

        //1.默认的安全管理器(安全管理器)
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        //2.IniRealm文件
        IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
        //3.默认的安全管理器添加Realm
        defaultSecurityManager.setRealm(iniRealm);
        //4.使用SecurityUtils工具生效安全管理器
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        //5.使用SecurityUtils工具获得主体
        Subject subject = SecurityUtils.getSubject();
        //6.构建账号token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jay", "123");
        //登录操作
        subject.login(usernamePasswordToken);
        System.out.println("是否登录成功：" + subject.isAuthenticated());
    }
}
